/**
 * @author Johannes Blersch
 * 
 * @since 28.05.11
 * @version 12.05.11 - Johannes - Fixed Bug that deleted players appear in the game.
 * 
 * Generates the player settings menu.
 */


package view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.annotation.Generated;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import model.Figure;


import control.*;

public class PlayerSettings extends JFrame{

	//variables	
	private int displayedPlayers = 2;
	private PlayerSettingsControl thePlayerSettingsControl;
	
	private GameControl gc;
	
	//layout elements
	private Container layoutContainer;	
	
	private JButton newPlayerButton, advancedSettingsButton, backButton, startButton;
	
	private JPanel selectPlayerPanel, backgroundPanel;
	private PlayerPanel[] playerPanels = new PlayerPanel[6]; 
	
	//icons for the buttons
	private ImageIcon newPlayerButImg = ImageCreator.createImageIcon("Images/plus.png");
	private ImageIcon advancedSettingsButImg = ImageCreator.createImageIcon("Images/Buttons/SE_EE.png");
	private ImageIcon backButImg = ImageCreator.createImageIcon("Images/Buttons/SE_ZURUUECK.png");
	private ImageIcon startButImg = ImageCreator.createImageIcon("Images/Buttons/SE_SPIELSTARTEN.png");
	private ImageIcon backgrd = ImageCreator.createImageIcon("Images/pattern/background.png");
	////////////////////////////////////////////////////////////
	//// Constructor										////
	////////////////////////////////////////////////////////////
	
	/**
	 * Generates the game menu window
	 */
	public PlayerSettings() 	{}
	
	public PlayerSettings(PlayerSettingsControl control, GameControl gc)	{
		super("Spielereinstellungen");		
		thePlayerSettingsControl = control;	//generating the association
		this.gc = gc;
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//creation of the default two player panels
		playerPanels[0] = new PlayerPanel(1,0, thePlayerSettingsControl);
		playerPanels[0].setBorder(BorderFactory.createTitledBorder("Spieler "+1));
		playerPanels[1] = new PlayerPanel(2,1, thePlayerSettingsControl);
		playerPanels[1].setBorder(BorderFactory.createTitledBorder("Spieler "+2));
				
		//Creation of the Buttons		
		newPlayerButton = new JButton(newPlayerButImg);		
		newPlayerButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		newPlayerButton.setContentAreaFilled(false);
		newPlayerButton.setActionCommand("newPlayer");
		newPlayerButton.addActionListener(thePlayerSettingsControl);
		
		advancedSettingsButton = new JButton(advancedSettingsButImg);
		advancedSettingsButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		advancedSettingsButton.setContentAreaFilled(false);
		advancedSettingsButton.setActionCommand("advancedSettings");
		advancedSettingsButton.addActionListener(thePlayerSettingsControl);
		
		backButton = new JButton(backButImg);
		backButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		backButton.setContentAreaFilled(false);
		backButton.setActionCommand("back");
		backButton.addActionListener(GameControl.getControlInstance());
		
		startButton = new JButton(startButImg);
		startButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		startButton.setContentAreaFilled(false);
		startButton.setActionCommand("start");
		startButton.addActionListener(this.gc);
		
		layoutContainer = getContentPane();		
		backgroundPanel = new JPanel(new BorderLayout());
	/*	{
			public void paint(Graphics g)
			{				
				g.drawImage(backgrd.getImage(),0,0,null);
				super.paint(g);
			}
		};*/
		
		//Panel for the player selection
		selectPlayerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		selectPlayerPanel.add(playerPanels[0]);
		selectPlayerPanel.add(playerPanels[1]);
		selectPlayerPanel.add(newPlayerButton);		
		backgroundPanel.add(selectPlayerPanel, BorderLayout.CENTER);
		
		//Panel for the control buttons
		JPanel buttonsPanel = new JPanel(new GridLayout(1,2));
				
		//Buttons on the left side
		JPanel leftButtonsPanel = new JPanel(new GridLayout(2,1));
		
		JPanel topPanel = new JPanel (new FlowLayout(FlowLayout.LEFT));
		topPanel.add(advancedSettingsButton);
		leftButtonsPanel.add(topPanel);
		
		JPanel bottomPanel = new JPanel (new FlowLayout(FlowLayout.LEFT));
		bottomPanel.add(backButton);
		leftButtonsPanel.add(bottomPanel);
		
		//Buttons on the right side
		JPanel rightButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		rightButtonsPanel.add(startButton);
		
		buttonsPanel.add(leftButtonsPanel);
		buttonsPanel.add(rightButtonsPanel);
		backgroundPanel.add(buttonsPanel, BorderLayout.SOUTH);
	
		layoutContainer.add(backgroundPanel);
		this.setResizable(false);
		this.setSize(1025,725);
	}	
	
	////////////////////////////////////////////////////////////
	//// Public Methods										////
	////////////////////////////////////////////////////////////
	
	/**
	 * Generates a PlayerPanel with the settings for a new Player.
	 */
	public void generateNewPlayerPanel(int playerNumber, int figureNumber) {		
		selectPlayerPanel.remove(newPlayerButton);
		playerPanels[playerNumber] = new PlayerPanel(playerNumber+1, figureNumber,thePlayerSettingsControl); //playerNumber+1 becuause in the programm the playercount starts with 0 
		selectPlayerPanel.add(playerPanels[playerNumber]);
		playerPanels[playerNumber].setBorder(BorderFactory.createTitledBorder("Spieler "+(playerNumber+1)));
		displayedPlayers++;
		if (displayedPlayers < 6)		{
			selectPlayerPanel.add(newPlayerButton);
		}		
		selectPlayerPanel.validate(); //necessary for adding the new Player Panel
		selectPlayerPanel.repaint();
//Testausgabe
		System.out.println("Spielerzahl vor inkrement:"+displayedPlayers);
		
//Testausgabe
		System.out.println("Spielerzahl nach inkrement:"+displayedPlayers);
	}
	
	/**
	 * Removes the PlayerPanel of the selected Player from the window.
	 * 
	 * @param number Number of the player who become removed.
	 */
	public void removePlayerPanel(int number) {
		selectPlayerPanel.remove(playerPanels[number]);
		playerPanels[number] = null;	//12.05.11 - Johannes - Added this line.
//Testausgabe
		System.out.println("Spielerzahl vor dekrement:"+displayedPlayers);
		displayedPlayers--;
//Testausgab
		System.out.println("Spielerzahl nach dekrement:"+displayedPlayers);
		if (displayedPlayers < 6)		{
			selectPlayerPanel.add(newPlayerButton);
		}
		selectPlayerPanel.validate(); //necessary for adding the new Player Panel
		selectPlayerPanel.repaint();		
	}
	
	////////////////////////////////////////////////////////////
	//// Get-Methods										////
	////////////////////////////////////////////////////////////
	
	/**
	 * Returns the array with the playerPanels
	 * 
	 * @return playerPanels array
	 */
	public PlayerPanel[] getPlayerPanels() {
		return playerPanels;
	}
}
